/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * @version     1.02
 * @author  	Andrzej Jan Taramina, Chaeron Corporation
 *  			andrzej@chaeron.com
 */

package com.chaeron.platform.common;


/**
 * A vector is an array of object references. The vector grows and shrinks
 * dynamically as objects are added and removed.
 * <p>
 * Here is an example showing a vector being used:
 *
 * <pre>
 * ...
 * Vector vec = new Vector();
 * vec.add(obj1);
 * vec.add(obj2);
 * ...
 * vec.insert(3, obj3);
 * vec.del(2);
 * if (vec.getCount() > 5)
 * ...
 * </pre>
 */
public class Vector
{
	Object items[];
	int count;
	
	/** Constructs an empty vector. */
	public Vector()
	{
		this(8);
	}
	
	/**
	 * Constructs an empty vector with a given initial size. The size is
	 * the initial size of the vector's internal object array. The vector
	 * will grow as needed when objects are added.
	 */
	public Vector(int size)
	{
		items = new Object[size];
	}
	
	/** Adds an object to the end of the vector. */
	public void add(Object obj)
	{
		if (count < items.length)
			items[count++] = obj;
		else
			insert(count, obj);
	}
	
	/** Inserts an object at the given index. */
	public void insert(int index, Object obj)
	{
		if (count == items.length)
		{
			// double size of items array
			Object newItems[] = new Object[items.length * 2];
			Vm.copyArray(items, 0, newItems, 0, count);
			items = newItems;
		}
		if (index != count)
			Vm.copyArray(items, index, items, index + 1, count - index);
		items[index] = obj;
		count++;
	}
	
	/** Deletes the object reference at the given index. */
	public void del(int index)
	{
		if (index != count - 1)
			Vm.copyArray(items, index + 1, items, index, count - index - 1);
		items[count - 1] = null;
		count--;
	}
	
	/** Returns the object at the given index. */
	public Object get(int index)
	{
		if (index >= count)
			index = items.length; // force an out of range error
		return items[index];
	}
	
	/** Sets the object at the given index. */
	public void set(int index, Object obj)
	{
		if (index >= count)
			index = items.length; // force an out of range error
		items[index] = obj;
	}
	
	/**
	 * Finds the index of the given object. The list is searched using a O(n) linear
	 * search through all the objects in the vector.
	 */
	public int find(Object obj)
	{
		for (int i = 0; i < count; i++)
			if (items[i] == obj)
				return i;
		return -1;
	}
	
	/** Returns the number of objects in the vector. */
	public int getCount()
	{
		return count;
	}
	
	/** Converts the vector to an array of objects. */
	public Object []toObjectArray()
	{
		Object objs[] = new Object[count];
		Vm.copyArray(items, 0, objs, 0, count);
		return objs;
	}
	
	/** Converts the vector to an array of Strings. */
	public String[] toStringArray()
	{
		String strs[] = new String[count];
		Vm.copyArray( items, 0, strs, 0, count );
		return strs;
	}
	
}